<template>
  <div id="LabInstrSetupForm">
    <div class="toolbartitle">基本信息</div>
    <el-scrollbar style="height: calc(100% - 40px)">
      <el-form ref="labreportform" :rules="formRules" :model="form" class="form-input-m baseForm" label-width="120px" inline-message inline>
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="功能编号" prop="instrId">
              <el-input v-model="form.funcCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="功能名称" prop="instrCode">
              <el-input v-model="form.funcName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="标题" prop="instrName">
              <el-input v-model="form.title"></el-input> </el-form-item></el-col>
          <el-col :span="12">
            <el-form-item label="图标">
              <el-input v-model="form.icon"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="请求URL" prop="instrCalled">
              <el-input v-model="form.apiUrl"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求方法" prop="instrMark">
              <el-input v-model="form.method"></el-input> </el-form-item></el-col>
        </el-row>

        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item label="功能脚本" prop="instrMark">
              <el-input v-model="form.script"></el-input> </el-form-item></el-col>
          <el-col :span="12">
            <el-form-item label="是否在用" prop="isUse">
              <el-switch v-model="form.status" inline-prompt active-text="是" inactive-text="否" :active-value="1" :inactive-value="0"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <div class="btnBox">
            <el-button type="primary" @click="handleAdd()">新建</el-button>
            <el-button type="primary" v-loading="showLoading" @click="save()">保存</el-button>
            <el-button type="default" @click="handleAdd()">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </div>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, PropType, onMounted } from 'vue';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
// 基本信息form表单
let form = reactive({
  id: '',
  funcCode: '',
  funcName: '',
  title: '',
  icon: '',
  apiUrl: '',
  method: '',
  script: '',
  status: false
});

// form表单校验规则
let formRules: any = {
  funcCode: [
    {
      required: true,
      message: '请输入功能编号',
      trigger: 'blur'
    }
  ],
  funcName: [
    {
      required: true,
      message: '请输入功能名称',
      trigger: 'blur'
    }
  ]
};

let labreportform = ref(null); //form表单标记ref

let showLoading = ref(false); //保存按钮

// 方法定义区 =============
defineExpose({ initPage, clearFormData });

// 左侧表格点击当前行传过来的数据
function initPage(row: any) {
  const el: any = proxy.$refs['labreportform'];
  el.clearValidate();
  if (row !== null) {
    for (let prop in row) {
      form[prop] = row[prop];
    }
  }
}

//清空数据
function clearFormData() {
  for (let key in form) {
    form[key] = '';
  }
  const el: any = proxy.$refs['labreportform'];
  el.resetFields(); //重置该表单项，将其值重置为初始值，并移除校验结果
}

// 新增按钮 + 重置按钮
function handleAdd() {
  clearFormData();
}

// 保存按钮
function save() {
  const el: any = proxy.$refs['labreportform'];
  if (!el) return;
  el.validate((valid: any) => {
    if (!valid) {
      return false;
    }
    if (form.id !== '' && form.id !== null) {
      showLoading.value = true;
      proxy.$http
        .post('/self/function/editFunction', form)
        .then((res: any) => {
          if (res.status.code === 1) {
            VxeMessage('修改功能信息成功', VxeNotifyType.success);
            proxy.$emit('getTableData'); //触发父组件重新获取表格数据
          } else {
            proxy.$emit('getTableData'); //触发父组件重新获取表格数据
            return;
          }
        })
        .catch((err: any) => {
          VxeMessage('修改功能信息异常', VxeNotifyType.error);
        });
    } else {
      proxy.$http
        .post('/self/function/addFunction', form)
        .then((res: any) => {
          if (res.status.code === 1) {
            VxeMessage('新增功能信息成功', VxeNotifyType.success);
            proxy.$emit('getTableData'); //触发父组件重新获取表格数据
          } else {
            proxy.$emit('getTableData'); //触发父组件重新获取表格数据

            return;
          }
        })
        .catch((err: any) => {
          VxeMessage('新增功能信息异常', VxeNotifyType.error);
        });
    }
    showLoading.value = false;
    clearFormData(); //清空数据
  });
}
let employeeSelectRef: any = ref(null);

let visibleOutReporter: any = ref(false);

</script>

<style lang="scss" scoped>
#LabInstrSetupForm {
  height: 100%;

  :deep(.el-scrollbar__view) {
    height: 100%;
  }
  .baseForm {
    height: calc(100% - 40px);

    .el-row {
      margin-left: 10px !important;
      margin-right: 0 !important;
    }

    .form-footer {
      margin-left: 130px;
    }

    .el-form-item--large {
      margin-bottom: 4px;
    }
  }
  .btnBox {
    width: 100%;
    margin-left: 140px;
  }
}
</style>
